package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.pojo.Tag;
import com.itheima.service.BlogService;
import com.itheima.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/tag")
public class TagController {
    @Autowired
    private BlogService blogService;

    @Autowired
    private TagService tagService;

    /**
     * 根据tagUid获取博客内容
     * @param tagUid
     *
     * @return
     */
    @GetMapping("/getArticleByTagUid")
    public String getArticleByTagUid(String tagUid){
        Integer currentPage = 1;
        Integer pageSize = 10;
        Page<Blog> blogContents = blogService.getArticleByTagUid(tagUid,currentPage,pageSize);
        Map<String,Object> map = new HashMap<>();
        map.put("records",blogContents);
        map.put("total",blogContents.getTotal());
        map.put("size",10);
        map.put("current",1);
        map.put("orders",new int[0]);
        map.put("optimizeCountSql",true);
        map.put("isSearchCount",true);
        return JSON.toJSONString(new Result<>("success",map));
    }

    @GetMapping("/getTagList")
    public String getTagList(){
        List<Tag> tags = tagService.getTagList();
        return JSON.toJSONString(new Result<>("success",tags));
    }
}
